﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.RAL.Access;
using Empires.RAL.ORM;

namespace Empires.RAL.Pump
{
    public class AppUserDataPump : IAppUserAccess
    {
        /// <summary>
        /// 返回用户身份
        /// </summary>
        /// <param name="username"></param>
        /// <returns>如果不存在该用户，返回-1</returns>
        public int GetUserStatus(string username)
        {
            if (String.IsNullOrEmpty(username))
            {
                return -1;
            }
            using (mysql_empiresEntities ctx = new mysql_empiresEntities())
            {
                var user = (from c in ctx.tb_user
                         where c.c_vc_username == username
                         select new { c.c_i_status }).FirstOrDefault();
                if (user != null)
                {
                    return user.c_i_status;
                }
                return -1;
            }
        }

        public T GetUserEntity<T>(string username) where T : class
        {
            if (String.IsNullOrEmpty(username))
            {
                return null;
            }
            using (mysql_empiresEntities ctx = new mysql_empiresEntities())
            {
                var user = (from c in ctx.tb_user
                            where c.c_vc_username == username
                            select c).FirstOrDefault();
                if (user != null && user is T)
                {
                    return (T)Convert.ChangeType(user, typeof(T));
                }
                return null;
            }
        }

        public int NewUserRegister(string username, string password,string nickname, bool sex, DateTime birthday, string contact, string appellaction, string address)
        {
            using (mysql_empiresEntities ctx = new mysql_empiresEntities())
            {
                if (!ctx.tb_user.Any(c => c.c_vc_username == username))
                {
                    ctx.AddTotb_user(
                        new tb_user()
                        {
                            c_b_isLock = false,
                            c_vc_username = username,
                            c_i_status = 0,
                            c_b_opened = false,
                            c_b_sex = sex,
                            c_nvc_nickname = nickname,
                            c_dt_birthday = birthday,
                            c_nvc_address = address,
                            c_nvc_appellaction = appellaction,
                            c_vc_contact = contact,
                            c_vc_password = password
                        });
                    return ctx.SaveChanges();
                }
                return 0;
            }
        }

        public int LockUser(string username)
        {
            if (String.IsNullOrEmpty(username))
            {
                return -1;
            }
            using (mysql_empiresEntities ctx = new mysql_empiresEntities())
            {
                var user = (from c in ctx.tb_user
                           where c.c_vc_username == username
                           select c).FirstOrDefault();
                if (user != null)
                {
                    user.c_b_isLock = true;
                    return ctx.SaveChanges();
                }
                else
                {
                    return -1;
                }
            }
        }

        public int UpdateUserInfo(string username, string password, bool sex, DateTime birthday, string contact, string appellaction, string address)
        {
            using (mysql_empiresEntities ctx = new mysql_empiresEntities())
            {
                var user = (from c in ctx.tb_user
                            where c.c_vc_username == username
                            select c).FirstOrDefault();
                if (user != null)
                {
                    user.c_vc_password = password;
                    user.c_b_sex = sex;
                    user.c_dt_birthday = birthday;
                    user.c_vc_contact = contact;
                    user.c_nvc_appellaction = appellaction;
                    user.c_nvc_address = address;
                    return ctx.SaveChanges();
                }
                return -1;
            }
        }


        public string GetUserNickname(string username)
        {
            using (mysql_empiresEntities ctx = new mysql_empiresEntities())
            {
                var user = ctx.tb_user.Where(c => c.c_vc_username == username).FirstOrDefault();
                if (user == null)
                {
                    return "游客";
                }
                return user.c_nvc_nickname;
            }
        }
        /// <summary>
        /// 存在返回1，否则返回0
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public int UserIsExists(string username)
        {
            using (mysql_empiresEntities ctx = new mysql_empiresEntities())
            {
                return ctx.tb_user.Any(c => c.c_vc_username == username) ? 1 : 0;
            }
        }
    }
}
